package com.xhl.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.xhl.service.ProductImageService;
import com.xhl.util.SpringContextUtil;
import com.xhl.util.Utils;
import com.xhl.vo.ProductImageVO;

@Controller
@RequestMapping("/productImg")
public class ProductImgController {

    @Autowired
    ProductImageService productImageService;

    @GetMapping(value = "/delete", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteProductImage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String piid = req.getParameter("piid");
        if (piid == null) {
            return "异常";
        }
        System.out.println(piid);
        productImageService.deleteProductImage(piid);
        return "删除成功" + piid;
    }

    @GetMapping(value = "/queryImg", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public void queryImg(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String piid = req.getParameter("piid");
        String type = req.getParameter("type");
        System.out.println(piid + " " + type);
        if (piid == null) {
            return;
        }
        if (type.equals("type_single")) {
            String filePath = "D:\\img\\productSingle\\" + piid + ".jpg";
            Utils.showImg(resp, filePath);
        } else if (type.equals("type_detail")) {
            String filePath = "D:\\img\\productDetail\\" + piid + ".jpg";
            Utils.showImg(resp, filePath);
        }
    }

    @PostMapping(value = "/add", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public void addProductImage(String type, String pid, String cid, MultipartFile filepath, HttpServletRequest req,
                                HttpServletResponse resp) throws IOException {
        System.out.println(type + "  ---  " + pid);
        if (pid == null) {
            return;
        }
        ProductImageVO pivo = SpringContextUtil.getBean(ProductImageVO.class);
        pivo.setPid(Integer.valueOf(pid));
        pivo.setType(type);
        productImageService.addProductImage(pivo);

        Long piid = productImageService.queryMax();

        String imgname = piid + ".jpg";                        //新图片名称

        if (type.equals("type_single")) {
            String realPath = "D:\\img\\productSingle";
            Utils.upload(filepath, realPath, imgname);
        } else if (type.equals("type_detail")) {
            String realPath = "D:\\img\\productDetail";
            Utils.upload(filepath, realPath, imgname);
        }
        resp.sendRedirect("http://localhost:8080/#/admin/productImageList?pid=" + pid + "&cid=" + cid);
    }

    @GetMapping(value = "/query", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String queryProductImage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pid = req.getParameter("pid");
        String type = req.getParameter("type");
        if (pid == null) {
            return "异常";
        }
        List<ProductImageVO> pList = productImageService.queryProductImage(pid, type);
        String json = JSON.toJSONString(pList);
        return json;
    }
}
